import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

declare var $: any;
@Component({
	selector: 'sa-login-log',
	templateUrl: './official-bind.component.html',

})
export class OfficialBindComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	tableParam:any = {
		searchKey: '',
		startTime: '',
		endTime: ''
	}
	isShowDelete=false; // 默认不显示删除按钮
	sortType = '2'
	dateType = '1'
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.initBtn();
		this.load();
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_wx:bind:login')));
		if(!btnData) {
			return
		};
		for(let item of btnData) {
			if(item.sourceKey === "wx-bind:login-log:deleteBtn" && item.parentId) {
				this.isShowDelete = true;
			}
		}
	}
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:common.stringTrim(this.tableParam.searchKey),
				startTime: this.tableParam.startTime,
				endTime: this.tableParam.endTime,
				sortType: this.sortType,
				dateType: this.dateType
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("wxPublic/wxPublicUserList.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				for (let index = 0; index < data.exhibitDatas.length; index++) {
					const item = data.exhibitDatas[index];
					if(item.sex=='1'){
						item.sexStr = "男"
					}else if(item.sex=='2'){
						item.sexStr = "女"
					}else{
						item.sexStr = "未知"
					}
				}
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.tableParam.startTime = time.startTime;
		this.tableParam.endTime = time.endTime;
	} 
	// 删除登录日志
	deleteRow(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let parameters = {
				ids: row.id,
				type: "3",
			}
			this.jsonApiService.http_obj("appointmentRepairCar/batchDeleted.html", parameters).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	rowData:any = {} // 那一条记录
	bindDetailTableData:any=[] // 绑定详情的信息
	// factoryOnlinePageSize: number = 10; // 厂区在线表的页的条数
	// factoryOnlineCurPage: any = 1;      // 当前页码
	// factoryOnlinetotalCount: number = 0; // 分页总条数
	// 展示平台绑定信息
	showRow(row,modal){
		this.rowData = JSON.parse(JSON.stringify(row))
		let list = this.rowData.platformInfoList
		if(!list||list.length==0){
			this.jsonApiService.message("该用户暂无平台绑定数据");
			return;
		}
		modal.show()
		this.bindDetailTableData = JSON.parse(JSON.stringify(list));

	}
	// 关闭厂区在线弹框
	cancelFactoryOnline(modal) {
		modal.hide();
		this.bindDetailTableData=[]
	}

	// 解绑厂区
	unbindRow(row){
		this.jsonApiService.confirm_message("此操作将解绑该用户与系统,确定要解绑?").then(() => {
			let index = this.bindDetailTableData.indexOf(row);
			this.bindDetailTableData.splice(index,1)
			
			for (let index = 0; index < this.tableData.length; index++) {
				const tableRow = this.tableData[index];
				if(tableRow.id== this.rowData.id){
					tableRow.platformInfoList = this.bindDetailTableData
				}
			}
			let reqdata = {
				id:this.rowData.id,
				platformInfoJson:JSON.stringify(this.bindDetailTableData)
			}
			this.jsonApiService.http_obj("wxPublic/unBundling.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				// this.load();
			})
		})
	}
}